package cn.jjzd.info.service;

import cn.jjzd.info.mapper.CategoryMapper;
import cn.jjzd.info.pojo.Category;
import org.bouncycastle.est.CACertsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper sysCategoryMapper;

    /**
     * 根据父ID查询所有子分类列表
     * @param pid
     * @return
     */
    public List<Category> queryCategoriesByPid(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        category.setRecordStatus(true);
        return sysCategoryMapper.select(category);
    }

    /**
     * 根据分类ID查询分类信息
     * @param categoryId
     * @return
     */
    public Category queryNameById(Long categoryId) {
        return sysCategoryMapper.selectByPrimaryKey(categoryId);
    }


    /**
     * 根据末级分类查询所有分类
     * @param cid
     * @return
     */
    public String queryNamesById(Long cid) {
        Category sysCategory = this.sysCategoryMapper.selectByPrimaryKey(cid);
        String categoryName = sysCategory.getName();
        return queryByIdAndName(categoryName,sysCategory.getParentId());
    }

    /**
     * 根据parentId查询分类
     * @param name
     * @param parentId
     * @return
     */
    public String queryByIdAndName(String name,Long parentId){
        if(parentId.longValue() != 0 ){
            Category category = this.sysCategoryMapper.selectByPrimaryKey(parentId);
            name = category.getName() +" -> "+ name;
            return queryByIdAndName(name,category.getParentId());
        }
        return name;
    }


    /**
     * 根据末级分类查询所有分类
     * @param cid
     * @return
     */
    public List<Category> queryNamesByCid(Long cid) {
        Category sysCategory = this.sysCategoryMapper.selectByPrimaryKey(cid);
        List<Category> categorys = new ArrayList<>();
        categorys.add(sysCategory);
        return queryNamesById(categorys,sysCategory.getParentId());
    }

    /**
     * 根据parentId查询分类
     * @param
     * @param parentId
     * @return
     */
    public List<Category> queryNamesById(List<Category> categorys, Long parentId){
        if(parentId.longValue() != 0 ){
            Category category = this.sysCategoryMapper.selectByPrimaryKey(parentId);
            categorys.add(category);
            Collections.reverse(categorys);
            return queryNamesById(categorys,category.getParentId());
        }
        return categorys;
    }

    /**
     * 根据parentId查询分类
     * @param paramMap
     * @return
     */
    public Map<String,Object> queryNamesByMap(Map<String,Object> paramMap){
        Long cid = (Long)paramMap.get("cid");
        String cname = (String)paramMap.get("cname");
        String cids = (String)paramMap.get("cids");
        Category category = this.sysCategoryMapper.selectByPrimaryKey(cid);
        if(category.getParentId().longValue() != 0 ){
            Map<String,Object> map = new HashMap<>();
            category = this.sysCategoryMapper.selectByPrimaryKey(category.getParentId());
            cname = category.getName() +" -> "+ cname;
            cids =  category.getId() + "," + cids;
            map.put("cid",category.getId());
            map.put("cname",cname);
            map.put("cids",cids);
            return queryNamesByMap(map);
        }
        return paramMap;
    }

    /**
     * 根据末级分类查询对应父分类下的所有子分类
     * @param cid
     * @return
     */
    public List<Category> queryCategoryList(Long cid) {
        Category category = sysCategoryMapper.selectByPrimaryKey(cid);
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parentId",0);
        criteria.andEqualTo("parentId",category.getParentId());
        example.setOrderByClause("sort asc ");
        return sysCategoryMapper.selectByExample(example);
    }

   /* public List<Category> queryByParentId(Long parentId) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parentId",0);
        criteria.andEqualTo("parentId",parentId);
        example.setOrderByClause("sort asc ");
        return sysCategoryMapper.selectByExample(example);
    }*/

    /**
     * 保存或编辑分类信息
     * @param category
     */
    public void saveCategory(Category category) {
        Category orginCategory = sysCategoryMapper.selectByPrimaryKey(category.getId());
        if(null != orginCategory){
            orginCategory.setName(category.getName());
            orginCategory.setDescription(category.getName());
            orginCategory.setUpdateCount(orginCategory.getUpdateCount()+1);
            orginCategory.setUpdateDate(new Date());
            orginCategory.setUpdaterId(-1L);
            sysCategoryMapper.updateByPrimaryKey(orginCategory);
        }else {
            category.setId(null);
            category.setDescription(category.getName());
            category.setType("article");
            category.setRecordStatus(true);
            category.setCreateDate(new Date());
            category.setCreatorId(-1L);
            category.setUpdateDate(category.getCreateDate());
            category.setUpdateCount(0);
            category.setUpdaterId(-1L);
            sysCategoryMapper.insertSelective(category);
            category.setUrl(category.getId()+".html?cid="+category.getId());
            sysCategoryMapper.updateByPrimaryKey(category);
        }
    }

    /**
     * 根据ID删除分类
     * @param id
     */
    public void deleteById(Long id) {
        Category orginCategory = sysCategoryMapper.selectByPrimaryKey(id);
        orginCategory.setRecordStatus(false);
        sysCategoryMapper.updateByPrimaryKey(orginCategory);
    }
}
